#coding:utf-8
'''
Created on 2012-7-16

@author: wb_suoyao.shen
'''
import datetime
from django.shortcuts import get_object_or_404
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from ssyblog.photos.models import Album
from ssyblog.photos.forms import AlbumForm

def queryAlbums(request):
    album_list = Album.objects.all();
    nav_parent = 'Album Manage'
    nav_child = 'Albbum List'
    current_nav = 'album_manage'
    return render_to_response('cms/photos/album_list.html',locals())

def addAlbum(request):
    nav_parent = 'Album Manage'
    nav_child = 'Albbum List'
    current_nav = 'album_manage'
    return render_to_response('cms/photos/album_add.html',locals())

@csrf_exempt
def saveAlbum(request):
    if request.method == 'POST':
        form = AlbumForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.create_time = datetime.datetime.now()
            obj.modify_time = datetime.datetime.now()
            obj.save()
            tips = 'create album success'
        else:
            tips = 'create album failed'
    nav_parent = 'Album Manage'
    nav_child = 'Albbum List'
    current_nav = 'album_manage'
    return render_to_response('cms/photos/album_add.html',locals())

def editAlbum(request,albumId):
    if albumId:
        album = Album.objects.get(pk=albumId)
    nav_parent = 'Album Manage'
    nav_child = 'Albbum Edit'
    current_nav = 'album_manage'
    return render_to_response('cms/photos/album_edit.html',locals())

@csrf_exempt
def updateAlbum(request,albumId):
    album_to_update = get_object_or_404(Album, pk = albumId)
    form = AlbumForm(request.POST,instance=album_to_update)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.modify_time = datetime.datetime.now()
        obj.save()
    return queryAlbums(request)
        